/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.yunyichina.provider.framework.annotation.validate;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Get field value by java reflect
 *
 * @author jiangtch
 */
public class GetFiledValue {
    private static final Logger logger = LoggerFactory.getLogger(GetFiledValue.class);

    /**
     * get the specify field or property value
     *
     * @param filter validated object
     * @param field  validated field or property
     * @return field or property value
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static Object getField(AnnotationValidable filter, String field)
            throws NoSuchMethodException, InvocationTargetException,
            IllegalAccessException {
        if (logger.isTraceEnabled()) {
            logger.trace(GetFiledValue.class.getName(), "getField()");
        }
        String firstLetter = field.substring(0, 1).toUpperCase();
        String getMethodName = "get" + firstLetter + field.substring(1);
        Method getMethod = filter.getClass().getMethod(getMethodName);
        Object returnValue = getMethod.invoke(filter);
        return returnValue;
    }

}
